package flashcardtest;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Transaction;

public class EditDictionaryServlet extends HttpServlet implements Servlet {
	
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// make sure we can get special characters
		//if (request.getCharacterEncoding() == null) {
		//	request.setCharacterEncoding("UTF-8");
		//}
		
		// read data from form
		String newKey1 = request.getParameter("key1");
		String newKey2 = request.getParameter("key2");
		String mode = request.getParameter("mode");

		// get reference to active dictionary
		FlashCardSet localset = (FlashCardSet)request.getSession().getAttribute("flashcardset");

		// test for empty set
		if (localset == null) {
			localset = new FlashCardSet("error", "k1", "k2");
		}
			
		// reset message
		request.getSession().setAttribute("message", "");

		if (mode.equals("Add Word")) {
			try {// add word to dictionary
				localset.addCard(newKey1, newKey2);
				request.getSession().setAttribute("message", newKey1 + "::" + newKey2 + " successfully added to set:" + localset.getTitle());
			}
			catch (NullPointerException e) {
				request.getSession().setAttribute("message", "invalid dictionary loaded");
			}
		}
		else if (mode.equals("Delete Entry")) {
			String input = request.getParameter("Dictionary Contents");
			int divider = input.indexOf("::");
			int length = input.length();
			String word1 = input.substring(0, divider);
			String word2 = input.substring(divider + 2, length);
			localset.remove(word1, word2);
		}
		else if (mode.equals("Order by Key 1")) {
			//localset.sort(1);
		}
		else if (mode.equals("Order by Key 2")) {
			//localset.sort(2);
		}
		else if (mode.equals("Delete Dictionary")) {
			localset.destroy();

			// update Session attributes
			request.getSession().removeAttribute("flashcardset");
			
			// redirect to main page
			getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
			return;
		}
		else {
			// default behavior
		}
		
		// redirect to edit page
		response.sendRedirect("/edit.jsp");

//		getServletContext().getRequestDispatcher("/edit.jsp").forward(request, response);


	}


}
